﻿using System.ComponentModel;
using Foundation;

namespace Poet.ViewModels
{
    /// <summary>
    /// View model for go to line dialog.
    /// </summary>
    public class GoToLineViewModel : ViewModel, IDataErrorInfo
    {
        /// <summary>
        /// Gets or sets the line number.
        /// </summary>
        /// <value>
        /// The line number.
        /// </value>
        public int LineNumber
        {
            get { return Get(() => LineNumber); }
            set { Set(() => LineNumber, value); }
        }

        /// <summary>
        /// Gets or sets the min line number.
        /// </summary>
        /// <value>
        /// The min line number.
        /// </value>
        public int MinLineNumber { get; set; }

        /// <summary>
        /// Gets or sets the max line number.
        /// </summary>
        /// <value>
        /// The max line number.
        /// </value>
        public int MaxLineNumber { get; set; }

        /// <summary>
        /// Gets an error message indicating what is wrong with this object.
        /// </summary>
        /// <returns>An error message indicating what is wrong with this object. The default is an empty string ("").</returns>
        public string Error
        {
            get { return IsValid(PropertyNameProvider.GetPropertyName(() => LineNumber)); }
        }

        /// <summary>
        /// Gets the error message for the property with the given name.
        /// </summary>
        /// <returns>The error message for the property. The default is an empty string ("").</returns>
        public string this[string columnName]
        {
            get { return IsValid(columnName); }
        }

        private string IsValid(string propertyName)
        {
            if (propertyName == PropertyNameProvider.GetPropertyName(() => LineNumber))
            {
                if (MinLineNumber > LineNumber || LineNumber > MaxLineNumber)
                {
                    var errorMessage = Localizing.ProvideValue("ValueMustBeInRange");
                    return string.Format("{0}: [{1}-{2}]", errorMessage, MinLineNumber, MaxLineNumber);
                }
            }

            return null;
        }
    }
}
